package com.event.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.event.exception.CategoryException;
import com.event.mapper.CategoryMapper;
import com.event.pojo.entity.Category;
import com.event.service.CategoryService;
import com.event.utils.ThreadLocalUtil;
import com.google.common.base.Strings;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author ihuwe
 * @description 针对表【category】的数据库操作Service实现
 * @createDate 2023-12-21 15:04:25
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
	implements CategoryService {
	
	@Override
	public List<Category> getCategoryList(Integer id) {
		return list(Wrappers.<Category>lambdaQuery().eq(id != null, Category::getCreateUser, id));
	}
	
	@Override
	public void addCategory(Category category) {
		if (list(category) != null && !list(category).isEmpty()) {
			throw new RuntimeException(CategoryException.CATEGORY_EXIST);
		}
		category.setCreateTime(LocalDateTime.now());
		category.setUpdateTime(LocalDateTime.now());
		category.setCreateUser(ThreadLocalUtil.get());
		save(category);
	}
	
	@Override
	public void updateCategory(Category category) {
		if (list(category) != null && !list(category).isEmpty()) {
			throw new RuntimeException(CategoryException.CATEGORY_EXIST);
		}
		category.setUpdateTime(LocalDateTime.now());
		category.setCreateUser(ThreadLocalUtil.get());
		updateById(category);
	}
	
	
	private List<Category> list(Category category) {
		return list(Wrappers.<Category>lambdaQuery()
			.eq(ThreadLocalUtil.get() != null, Category::getCreateUser, ThreadLocalUtil.get())
				.and(e-> e.eq(!Strings.isNullOrEmpty(category.getCategoryName()), Category::getCategoryName,
				category.getCategoryName()).or().eq(!Strings.isNullOrEmpty(category.getCategoryAlias()), Category::getCategoryAlias,
				category.getCategoryAlias())));
	}
}




